<?php
namespace Api\Controller;

use Common\Controller\BaseController;

class ApiController extends BaseController
{
    /**
     * 接口的入口方法
     */
    public function index()
    {
        $resign = $_REQUEST['sign'];
        $recode = $_REQUEST['code'];
        $remsg = $_REQUEST['msg'];
        $file = $_FILES;        //文件不加密

        if (strpos($resign, "%") !== false) {
            $resign = urldecode($resign);
        }
        if (strpos($recode, "%") !== false) {
            $recode = urldecode($recode);
        }
        if (strpos($remsg, "%") !== false) {
            $remsg = urldecode($remsg);
        }

        $plainjson = getparmtoplain($resign, $recode, $remsg); //把接受到的密文转化成明文
        $plainarr1 = json_decode($plainjson, 1);        //转化成数组进行相关业务逻辑
        $plainarr = $plainarr1[0];
        $this->do_interface($plainarr, $file);
    }


    function do_interface($plainarr, $file)
    {
        $action = $plainarr['action']; //处理业务逻辑的方法名
        //判断当前类里面是否存在该方法
        if (method_exists($this, $action)) {
            $this->$action($plainarr, $file);
        } else {
            $rt['slay'] = "0";
            $rt['msg'] = "没有找到对应方法！";
            $returnjson = json_encode($rt);
            sendtoclient($returnjson);
            die;
        }
    }


    /*
     * 这里是第一个方法
     */
    public function test($plainarr, $file)
    {
        $arr = array(
            'sta' => "1",
            'msg' => $plainarr,
            'file' => $file,
        );
        $returnjson = json_encode($arr);
        sendtoclient($returnjson);
    }

    /*
     * 这里是第二个方法
     */
    public function test1($plainarr, $file)
    {
        $arr = array(
            'sta' => "1",
            'msg' => $plainarr,
            'file' => $file,
        );
        $returnjson = json_encode($arr);
        sendtoclient($returnjson);
    }










    /**
     * 模拟客户端请求方法
     */
    public function ajax_demo()
    {
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $sendurl =$protocol.$_SERVER['HTTP_HOST']."/".U('Api/index');
        $sendjson=file_get_contents("php://input");
        $returnjson = sendtointerface($sendurl, $sendjson);   //不包含文件上传示例
        echo $returnjson;die;
    }



    /**
     * 接口说明文档
     */
    public function help()
    {
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $sendurl =$protocol.$_SERVER['HTTP_HOST']."/".U('Api/index');
        $this->assign("sendurl",$sendurl);
        $this->display();
    }


}